package logicaPresentacion;

 import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import logicaNegocio.Asignatura;
import logicaNegocio.GestionAdministrador;
import logicaNegocio.Grado;
import logicaNegocio.Pertenece;
import logicaNegocio.Profesor;



public class FormularioAsignaturaGrado extends JInternalFrame implements ActionListener
{
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 4566498807430129781L;
    private JButton btnCancelar,btnGuardar;
    private GestionAdministrador GAd;    
	private JLabel jlbAsignatura,jlbGrado;   
	private JComboBox<String>   jcbAsignatura,jcbGrado;
	private Box boxVCamposVentana; 
	private JPanel panBotones; 
	private ClaseAObservar observable;
    
    public FormularioAsignaturaGrado (GestionAdministrador GAd, ClaseAObservar observable)
    {	    	
        super("Introduzca la relacion entre el grado y la asignatura");
        this.GAd = GAd;
        this.observable = observable;
        
        setSize(400,400);
        Box boxVContenidoVerticalVentana = Box.createVerticalBox();
        Box boxHAsignatura =  Box.createHorizontalBox();
        jlbAsignatura= new JLabel("Asignatura ");   jcbAsignatura = new JComboBox<String>();
        for(int i = 0 ; i<GAd.getAsignaturas().size(); i++)
		{
		String Nombre = GAd.getNombreAsignatura(i);
        jcbAsignatura.addItem(Nombre);
        }
        boxHAsignatura.add(Box.createHorizontalStrut(10));
        boxHAsignatura.add(jlbAsignatura);
        boxHAsignatura.add(Box.createHorizontalStrut(40));
        boxHAsignatura.add(jcbAsignatura);
        JPanel panAsignatura =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panAsignatura.add(boxHAsignatura);

        Box boxHGrado =  Box.createHorizontalBox();
        
        jlbGrado = new JLabel("Grado ");   jcbGrado = new JComboBox<String>();
        Iterator<Entry<String, Grado>> it = GAd.getGrados().entrySet().iterator();
	
		while (it.hasNext()) 
	    {
	    Entry<String, Grado> e = it.next();
        String Nombre = GAd.getGrado(e.getKey());
        jcbGrado.addItem(Nombre);
        }
		boxHGrado.add(Box.createHorizontalStrut(10));
		boxHGrado.add(jlbGrado);
		boxHGrado.add(Box.createHorizontalStrut(40));
		boxHGrado.add(jcbGrado);
        JPanel panGrado =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panGrado.add(boxHGrado);
        
        
        boxVCamposVentana = Box.createVerticalBox();	        
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panAsignatura);
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panGrado);

        Box boxHBotones = Box.createHorizontalBox();

        btnGuardar = new JButton("Guardar");
        btnCancelar = new JButton("Cancelar");
        btnGuardar.addActionListener(this);
        btnCancelar.addActionListener(this);
        
        boxHBotones.add(btnGuardar);
        boxHBotones.add(Box.createHorizontalStrut(20));
        boxHBotones.add(btnCancelar);
        boxHBotones.add(Box.createHorizontalStrut(40));

        panBotones =  new JPanel(new FlowLayout(FlowLayout.RIGHT) );
        panBotones.add(boxHBotones);
        boxVContenidoVerticalVentana.add(Box.createVerticalStrut(30));
        boxVContenidoVerticalVentana.add(boxVCamposVentana);

        boxVContenidoVerticalVentana.add(Box.createVerticalStrut(20));
        boxVContenidoVerticalVentana.add(panBotones);

        add(boxVContenidoVerticalVentana);
        
        setVisible(true);  
    }


	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() instanceof JButton) 
		{
			JButton btnPulsado=(JButton)e.getSource();

			if (btnPulsado==btnGuardar) 
			{
				if(comprobarExiste() == false)
				{
					 GAd.anadirAsignaturaGrado(jcbAsignatura.getSelectedIndex(),jcbGrado.getSelectedIndex());
		          	 observable.setTabla();
		        	 dispose();
				}
				else
				{
					JOptionPane.showMessageDialog(this,"La combinacion de claves de asignatura y grado ya existe","Error",JOptionPane.ERROR_MESSAGE);
				}
          	 
            }
			
			else if (btnPulsado==btnCancelar) 
			{
				dispose();
			}					
		}
	}
   public boolean comprobarExiste()
   {
	   	Pertenece P = null;
    	boolean comprobacion = false;
    	Grado gg = null;
    	ArrayList<Pertenece>asignaturagrado = GAd.getAsignaturaGrado();
    	Asignatura a = GAd.getAsignaturas().get(jcbAsignatura.getSelectedIndex());

    	Map<String, Grado> grados = GAd.getGrados();
    	for (Map.Entry<String, Grado> entry : grados.entrySet())
		{

    		Grado g = grados.get(entry.getKey());
    		if (g.getNombreGrado().equals(jcbGrado.getSelectedItem()))
    		{
    			gg = g;
    		}
		}
    	
    	if(asignaturagrado.size()>0)
    	{
    		for (int i=0;i<asignaturagrado.size();i++)
    		{	
    			P = asignaturagrado.get(i);
    			if((P.getAsignatura().getID())==(a.getID()))
				{
    				if((P.getGrado().getID())==gg.getID())
					{
						comprobacion = true;
					}
				}
    		}	
    	}  	
    	return comprobacion;
    }
}